package com.jeesite.modules.isys.hander;

import cn.hutool.json.JSONUtil;
import com.jeesite.modules.isys.iac.entity.IACMeta;
import com.jeesite.modules.isys.iac.service.IACService;
import com.jeesite.modules.isys.xmqtt.ann.XSubscription;
import com.jeesite.modules.isys.xmqtt.hander.XHander;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
@XSubscription(topic = "testtopic/1")
public class IACHander implements XHander {


    @Qualifier("IACService")
    @Autowired
    private IACService iacService;
    @Override
    public int getOrder() {
        return 0;
    }

    @Override
    public MqttMessage hander(MqttMessage mqttMessage) {
        byte[] payload = mqttMessage.getPayload();
        String jsonStr = new String(payload);
        IACMeta iacMeta = JSONUtil.toBean(jsonStr, IACMeta.class);
        int insert = iacService.getBaseMapper().insert(iacMeta);
        if (insert >0 ){
            System.out.println("写入成功: "+iacMeta.getUuid());
        }else {
            System.out.println("写入失败: "+iacMeta.getUuid());
        }
        return null;
    }
}
